package lg.minecraft.plugin.worldprotect;

import lg.minecraft.plugin.library.LGPlugin;
import lg.minecraft.plugin.worldprotect.command.OpenCommand;
import lg.minecraft.plugin.worldprotect.command.MainCommand;
import lg.minecraft.plugin.worldprotect.command.ReloadCommand;
import lg.minecraft.plugin.worldprotect.config.Language;
import lg.minecraft.plugin.worldprotect.config.MainConfig;
import lg.minecraft.plugin.worldprotect.config.Worlds;
import lg.minecraft.plugin.worldprotect.listener.BlockEvent;
import lg.minecraft.plugin.worldprotect.listener.EntityEvent;
import lg.minecraft.plugin.worldprotect.listener.PlayerEvent;
import lg.minecraft.plugin.worldprotect.util.WorldSettings;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;

import java.util.HashMap;
import java.util.Map;

@Getter
public final class LGWorldProtect extends LGPlugin {

    @Getter private static final Map<String, WorldSettings> worldSettingsMap = new HashMap<>();

    @Getter private static LGWorldProtect instance;

    @Getter private static Language language;
    @Getter private static MainConfig mainConfig;
    @Getter private static Worlds worlds;

    @Override
    public void onEnable() {
        instance = this;
        language = new Language();
        mainConfig = new MainConfig();
        worlds = new Worlds();
        Bukkit.getPluginManager().registerEvents(new BlockEvent(), this);
        Bukkit.getPluginManager().registerEvents(new PlayerEvent(), this);
        Bukkit.getPluginManager().registerEvents(new EntityEvent(), this);
        MainCommand mainCommand = new MainCommand();
        mainCommand.registerSubCommand(new OpenCommand());
        mainCommand.registerSubCommand(new ReloadCommand());
    }

    @Override
    public void onDisable() {
        this.worlds.save();
    }

    public static WorldSettings getWorldSettings(String worldName) {
        WorldSettings worldSettings = worldSettingsMap.get(worldName);
        if (worldSettings == null) {
            worldSettings = new WorldSettings(worldName);
            worldSettingsMap.put(worldName, worldSettings);
        }
        return worldSettings;
    }

    public static WorldSettings getWorldSettings(World world) {
        return getWorldSettings(world.getName());
    }

    public static Material getIcon(WorldSettingType setting) {
        Material material = mainConfig.getIcons().get(setting);
        if (material == null) {
            material = Material.STONE;
        }
        return material;
    }

}
